package secondriver.jigsaw;

import java.util.Random;

import secondriver.jigsaw.layout.ImageGridLayout;
import secondriver.jigsaw.layout.ImageGridLayout.GameListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";

	private ImageGridLayout imageGridLayout;

	private TextView levelTextView;
	private TextView timesTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageGridLayout = (ImageGridLayout) findViewById(R.id.game_view);
		imageGridLayout.setOnGameListener(new MainGameListener());

		levelTextView = (TextView) findViewById(R.id.game_level);
		timesTextView = (TextView) findViewById(R.id.game_times);

		levelTextView.setText(getStringById(R.string.level)
				+ imageGridLayout.getGameLevel());
		timesTextView.setText(getStringById(R.string.time)
				+ imageGridLayout.getGameTimes());
	}

	class MainGameListener implements GameListener {

		@Override
		public void onNextLevel(final int nextLevel) {
			new AlertDialog.Builder(MainActivity.this)

					.setIcon(android.R.drawable.ic_dialog_info)
					.setTitle(R.string.gamewon)
					.setMessage(getBlessing())
					.setPositiveButton(R.string.next_level,
							new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									levelTextView
											.setText(getStringById(R.string.level)
													+ nextLevel);
									imageGridLayout.onNextLevel();
								}
							})
					.setNegativeButton(R.string.quit_btn_label,
							new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
							}).show();
		}

		@Override
		public void onGameOver() {

			new AlertDialog.Builder(MainActivity.this)
					.setIcon(android.R.drawable.ic_dialog_info)
					.setTitle(R.string.gameover)
					.setMessage(getBlessing())
					.setPositiveButton(R.string.restart, new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							imageGridLayout.onRestart();
						}
					})
					.setNegativeButton(R.string.quit_btn_label,
							new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
							}).show();
		}

		@Override
		public void onTimeChanged(int currentTime) {
			timesTextView.setText(getStringById(R.string.time) + currentTime);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		imageGridLayout.onPauseTimes();
	}

	@Override
	protected void onResume() {
		super.onResume();
		imageGridLayout.onResumeTimes();
	}

	private String getStringById(int resId) {
		return getResources().getString(resId);
	}

	private String getBlessing() {
		String[] blesses = getResources().getStringArray(R.array.blessing);
		Random random = new Random();
		return blesses[random.nextInt(blesses.length)];
	}
}
